<template>
 <el-card class="userinfo-container">
    <!-- 面板屑导航 -->
     <el-breadcrumb separator-class="el-icon-arrow-right" style="margin: 20px ">
      <el-breadcrumb-item :to="{ path: '/' }"
        ><i class="el-icon-s-home"></i> 首页</el-breadcrumb-item
      >
      <el-breadcrumb-item
        ><i class="el-icon-share"></i> 个人中心</el-breadcrumb-item
      >
      <el-breadcrumb-item
        ><i class="el-icon-share"></i> 基本资料</el-breadcrumb-item
      >
    </el-breadcrumb>
    <el-form :model="userDetail" :rules="infoRuels" ref="infoRef" label-width="100px" style="width:400px">
      <el-form-item label="登录名称">
        <el-input v-model="userDetail.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input v-model="userDetail.nickname"></el-input>
      </el-form-item>
      <el-form-item label="用户邮箱" prop="email">
        <el-input v-model="userDetail.email"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="userUpdateInfo">提交修改</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import { reqUpdateUserInfo } from '@/api/user'
import { mapState, mapActions } from 'vuex'
export default {
  name: 'UserInfo',
  computed: {
    ...mapState('user', ['userInfo'])
  },
  async created () {
    const res = await this.getUserInfo()
    console.log(res)
    this.userDetail = { ...res }
    console.log(this.userDetail)
  },
  // mounted () {
  //   console.log(this.userInfo)
  // },
  data () {
    return {
      userDetail: {},
      // 验证规则对象
      infoRuels: {
        nickname: [
          { required: true, message: '昵称不能为空', trigger: ['blur', 'change'] },
          { pattern: /^\S{1,10}$/, message: '昵称是1-10个非空字符', trigger: ['blur', 'change'] }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: ['blur', 'change'] },
          { type: 'email', message: '邮箱格式不正确', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    // 重置
    reset () {
      this.userDetail = { ...this.userInfo }
    },
    // 更新资料
    ...mapActions('user', ['getUserInfo']),
    // 提交
    userUpdateInfo () {
      this.$refs.infoRef.validate(async boo => {
        console.log(boo)
        if (boo) {
          const { data } = await reqUpdateUserInfo(this.userDetail)
          if (data.code !== 0) return this.$message.error(data.message)
          this.$message.success(data.message)
          this.$store.dispatch('user/getUserInfo')
        }
      })
    }
  }
}
</script>
